package org.onepiece.admin.common.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.onepiece.admin.common.errorcode.CommonErrorCode;
import org.onepiece.admin.common.errorcode.ServiceCode;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 *
 * @author doublelife
 * Date: 2019/3/20
 * Time: 15:50
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class CommonResult<T> implements Serializable {

    private static final long serialVersionUID = 5808228220166587328L;

    private Integer code = HttpStatus.OK.value();
    private String info = HttpStatus.OK.getReasonPhrase();
    private T result;

    public static <T> CommonResult<T> create(Integer code, String info, T result) {
        return new CommonResult<>(code, info, result);
    }

    public static <T> CommonResult<T> create(Integer code, String info) {
        return create(code, info, null);
    }

    public static <T> CommonResult<T> create(HttpStatus httpStatus) {
        return create(httpStatus.value(), httpStatus.getReasonPhrase());
    }

    public static <T> CommonResult<T> create(HttpStatus httpStatus, T result) {
        return create(httpStatus.value(), httpStatus.getReasonPhrase(), result);
    }

    public static <T> CommonResult<T> create(ServiceCode serviceCode) {
        return create(serviceCode.getCode(), serviceCode.getInfo());
    }

    public static <T> CommonResult<T> create(ServiceCode serviceCode, T result) {
        return create(serviceCode.getCode(), serviceCode.getInfo(), result);
    }

    public static <T> CommonResult<T> create(T result) {
        return create(HttpStatus.OK, result);
    }

    public static <T> CommonResult<T> ok(T result) {
        return create(result);
    }

    public static <T> CommonResult<T> error() {
        return create(CommonErrorCode.FAIL);
    }
}
